package com.bdqn.t382.filter;

import com.bdqn.t382.util.RedisUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class AuthenticationInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 从请求头中获取token
        String token = request.getHeader("Authorization");
        RedisUtil redisUtil = new RedisUtil();
        String name = redisUtil.getKey("token");
        // 放行登录和卡号校验接口
        if (request.getRequestURI().contains("/api/login") ){
            return true;
        }
        if (token.isEmpty() || token == null) {
            return false;
        }else if (token.equals(name)) {
            return true;
        }
        return false;
    }
}
